package com.example.speeddating.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.speeddating.annotaion.LoginAdmin;
import com.example.speeddating.annotaion.LoginUser;
import com.example.speeddating.annotaion.TokenCheck;
import com.example.speeddating.common.BaseResponse;
import com.example.speeddating.common.RespGenerator;
import com.example.speeddating.entity.Admin;
import com.example.speeddating.entity.TokenEntity;
import com.example.speeddating.entity.Topic;
import com.example.speeddating.entity.User;
import com.example.speeddating.service.TopicService;
import com.example.speeddating.tool.StringTools;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.statement.select.Top;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;


/**
 * @author tina
 * @since 2023-06-28 11:14:34
 */
@Slf4j
@RestController
public class TopicController {

    @Autowired
    private TopicService topicService;

    @GetMapping("/topic/hot_topics")
    public BaseResponse<List<Topic>> getHotTopics(){
        List<Topic> hotTopics = topicService.getHotTopics();
        return RespGenerator.returnOK(hotTopics);
    }


    /**
     * 所有话题
     * @param user
     * @param pageSize
     * @param pageNum
     * @return
     */
    @TokenCheck(roles = "user")
    @GetMapping("/topic/all")
    public BaseResponse<Page> getAllTopic(@LoginUser User user, @RequestParam(defaultValue = "10",name = "pageSize") int pageSize,
                                          @RequestParam(defaultValue = "1",name = "pageNum") int pageNum){

        Page<Topic> pageInfo = new Page<>(pageNum,pageSize);
        QueryWrapper<Topic> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("type",0);
        Page<Topic> page = topicService.selectAllTopic(pageInfo);
        return RespGenerator.returnOK(page);
    }

    /**
     * 所有话题
     * @param admin
     * @param pageSize
     * @param pageNum
     * @return
     */
    @TokenCheck(roles = "admin")
    @GetMapping("/admin/topic/all")
    public BaseResponse<Page> getAllTopic(@LoginAdmin Admin admin,@RequestParam String searchData, @RequestParam(defaultValue = "10",name = "pageSize") int pageSize,
                                          @RequestParam(defaultValue = "1",name = "pageNum") int pageNum){

       if (!StringTools.isEmpty(searchData)){
           pageNum = 1;  // if searchData is notNull set pageNum = 1,resolve null result and not filter before pageNum
       }
        Page<Topic> pageInfo = new Page<>(pageNum,pageSize);
        Page<Topic> allTopics = topicService.getAllTopics(pageInfo, searchData);
        return RespGenerator.returnOK(allTopics);
    }


    /**
     * 添加话题
     * @param topic
     * @return
     */
    @TokenCheck(roles = "admin")
    @PostMapping("/admin/topic/add")
    public BaseResponse<Topic> addTopic(@LoginAdmin Admin admin,@RequestBody Topic topic){
        topicService.save(topic);
        return RespGenerator.returnOK("添加成功");
    }

    /**
     * 修改
     * @param topic
     * @return
     */
    @TokenCheck(roles = "admin")
    @PutMapping("/admin/topic/update")
    public BaseResponse<Object> updateTopic(@RequestBody  Topic topic){
        log.info(topic.getBreif()+topic.getId()+topic.getName());
        return RespGenerator.returnOK(topicService.updateById(topic));
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/admin/topic/delete/{id}")
    public BaseResponse<Object> deleteTopic(@PathVariable int id){
        return RespGenerator.returnOK(topicService.removeById(id));
    }


    @RequestMapping("/user/joined_topic")
    @TokenCheck(roles = "user")
    public BaseResponse<Page> getUserJoinedTopic(@LoginUser TokenEntity tokenEntity, @RequestParam(defaultValue = "10",name = "pageSize") int pageSize,
                                                 @RequestParam(defaultValue = "1",name = "pageNum") int pageNum){

        Page<Topic> topicPage = new Page<>(pageNum,pageSize);
        return RespGenerator.returnOK(topicService.getUserJoinedTopic(topicPage,tokenEntity.getId()));
    }

}
